package com.lanxin.stu.controller;


import com.lanxin.result.ResponseResult;
import com.lanxin.stu.dto.ClassesAddDTO;
import com.lanxin.stu.dto.ClassesCheckDTO;
import com.lanxin.stu.dto.ClassesDTO;
import com.lanxin.stu.dto.ClassesSearchDTO;
import com.lanxin.stu.entity.Classes;
import com.lanxin.stu.mapstruct.ClassesMapStruct;
import com.lanxin.stu.service.IClassesService;
import com.lanxin.stu.vo.ClassesVO;
import com.lanxin.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cyh
 * @since 2025-09-17
 */
@RestController
@RequestMapping("/classes")
@Api(tags = "班级管理")
@Validated
public class ClassesController {

    @Autowired
    private IClassesService classesService;

    @Autowired
    private ClassesMapStruct classesMapStruct;

    @ApiOperation(value = "分页查询班级信息")
    @PostMapping(value = "/listClasses")
    public ResponseResult<PageVO<ClassesVO>> listClasses(@Validated @RequestBody ClassesSearchDTO classesSearchDTO) {
        PageVO<ClassesVO> pagevo = classesService.listPageClasses(classesSearchDTO);
        return ResponseResult.success(pagevo);
    }

    @ApiOperation(value = "添加班级信息")
    @PostMapping("/addClasses")
    public ResponseResult<Object> addClasses(@Validated @RequestBody ClassesAddDTO classesAddDTO) {
        boolean result = classesService.addClasses(classesAddDTO);
        if (result) {
            return ResponseResult.success("添加成功");
        }
        return ResponseResult.fail("添加失败");

    }


    @ApiOperation(value = "根据id查询班级信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "班级编号", required = true)
    })
    @GetMapping("/findClassesById/{id}")
    public ResponseResult<Object> findClassesById(
            @NotNull(message = "班级编号不能为空")
            @Positive(message = "班级编号不正确")
            @PathVariable("id") Integer id) {
        ClassesVO classesVO = classesService.findClassesById(id);
        return ResponseResult.success(classesVO);
    }


    @ApiOperation(value = "修改班级信息")
    @PutMapping("/updateClasses")
    public ResponseResult<Object> updateClasses(@Validated @RequestBody ClassesDTO classesDTO) {
        boolean result = classesService.updateClasses(classesDTO);
        if (result) {
            return ResponseResult.success("修改成功");
        }
        return ResponseResult.fail("修改失败");

    }


    @ApiOperation(value = "删除班级信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "班级编号", required = true)
    })
    @DeleteMapping("/deleteClasses/{id}")
    public ResponseResult<Object> deleteClasses(
            @NotNull(message = "班级编号不能为空")
            @Positive(message = "班级编号不正确")
            @PathVariable("id") Integer id) {
        boolean result = classesService.deleteClasses(id);
        if (result) {
            return ResponseResult.success("删除成功");
        }
        return ResponseResult.fail("删除失败");

    }

    @ApiOperation(value="检查班级名称是否可用")
    @PostMapping(value="/checkClassName")
    public ResponseResult<Object> checkClassName(
            @Validated @RequestBody ClassesCheckDTO classesCheckDTO){
        boolean result = classesService.checkClassName(classesCheckDTO);
        if (result) {
            return ResponseResult.success("班级名称可用");
        }
        return ResponseResult.fail("班级名称不可用");
    }

    @ApiOperation(value="查询所有班级(编号，名称)")
    @GetMapping(value="/selectClass")
    public ResponseResult<List<ClassesVO>> selectClass(){
        List<Classes> listClasses = classesService.list();
        List<ClassesVO> listClassesVO=classesMapStruct.toListVO(listClasses);
        return ResponseResult.success(listClassesVO);
    }
}
